package com.oracle.sbdb.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.oracle.sbdb.bo.BugInfoBo;
import com.oracle.sbdb.servlet.model.BugInformation;
import com.oracle.sbdb.util.JSONUtility;

/**
 * Servlet implementation class SelectGroupBug
 */
@WebServlet("/selectGroupBug")
public class SelectGroupBug extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public SelectGroupBug() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String employeeIDString = request.getParameter("employeeID");
		String reponseContent = selectGroupBug(employeeIDString);
		PrintWriter out = response.getWriter();
		out.write(JSONUtility.bean2Json(reponseContent));
		out.flush();
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JSONObject requestJson = JSONUtility.getRequestString(request);
		String employeeIDString = requestJson.getString("employeeID");
		String reponseContent = selectGroupBug(employeeIDString);
		PrintWriter out = response.getWriter();
		out.write(JSONUtility.bean2Json(reponseContent));
		out.flush();
		out.close();
	}
	
	private String selectGroupBug(String employeeIDString){
		List<BugInformation> bugList = null;
		try{
			if(employeeIDString != null){
				int employeeID = -1;
				try{
					employeeID = Integer.parseInt(employeeIDString.trim());
				}catch(Exception e){
					e.printStackTrace();
				}
				if(employeeID >= 0){
					bugList = BugInfoBo.INSTANCE.selectGroupBug(employeeID);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		JSONObject res = new JSONObject();
		res.put("Status", bugList == null ? "Fail" : "Success");
		res.put("Result", bugList);
		return res.toString();
	}
}
